package com.seari.custody.controller;

import com.seari.custody.bean.*;
import com.seari.custody.component.MinIoUtil;
import com.seari.custody.util.JwtTokenUtils;
import com.seari.custody.constant.MessageConstant;
import com.seari.custody.pojo.MessageBean;
import com.seari.custody.service.TechnicalAdviceService;
import com.seari.custody.util.UUIDUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.seari.custody.util.JwtTokenUtils.TOKEN_HEADER;

/**
 * 审核意见生成模板
 */

@Slf4j
@RestController
@RequestMapping("/Technical")
public class TechnicalAuditController {


    @Autowired
    private TechnicalAdviceService technicalAdviceService;

    @Autowired
    private MinIoUtil minIoUtil;

    /**
     * 技审意见生成——许可、征询类
     *
     * @param token
     * @param map
     * @return
     */
    @RequestMapping(value = "/saver")
    public MessageBean TechnicalAuditServer(@RequestHeader(value = TOKEN_HEADER, defaultValue = "") String token, @RequestBody Map<String, String> map) throws Exception {
        MessageBean messageBean = new MessageBean();
//        try {
            map.put("token", token);
            Map<String, Object> map1 = technicalAdviceService.technicalReviewcreate(map);
            String code = (String) map1.get("code");
            String message = (String) map1.get("message");
            String type = (String) map1.get("type");
            Object data = map1.get("data");
            messageBean.setCode(code);
            messageBean.setMessage(message);
            messageBean.setType(type);
            messageBean.setData(data);
//        } catch (Exception e) {
//            messageBean.setType(MessageConstant.FAIL_TYPE);
//            messageBean.setCode(MessageConstant.FAIL);
//            messageBean.setMessage("工程信息概括不符合规范要求，请修改后重新提交");
//        }
        return messageBean;
    }

    /**
     * 技审意见类型
     */
    @RequestMapping("/type")
    public MessageBean TechnicalAudittype() {
        MessageBean messageBean = new MessageBean();
        try {
            List<TechnicalType> a = technicalAdviceService.technicalType();
            messageBean.setType(MessageConstant.SUCCESS_TYPE);
            messageBean.setCode(MessageConstant.SUCCESS);
            messageBean.setMessage("查询技审类型成功");
            messageBean.setData(a);
        } catch (Exception e) {
            e.printStackTrace();
            messageBean.setType(MessageConstant.FAIL_TYPE);
            messageBean.setCode(MessageConstant.FAIL);
            messageBean.setMessage("查询技审类型失败");
        }
        return messageBean;
    }


    /**
     * 许可类的技审列表
     */
    @RequestMapping("/permissionList")
    public MessageBean technicalListed(@RequestBody Map<String, String> map) {
        MessageBean messageBean = new MessageBean();
        try {
            String reviewType = map.get("reviewType");
            //许可
            if (reviewType.equals("1")) {
                List<TemplateWord> a = technicalAdviceService.technicalListed(map);
                messageBean.setType(MessageConstant.SUCCESS_TYPE);
                messageBean.setCode(MessageConstant.SUCCESS);
                messageBean.setMessage("查询许可类列表成功");
                messageBean.setData(a);
            }
            //征询
            if (reviewType.equals("2")) {
                String uuid = map.get("uuid");
                String uploadingId = map.get("type");
                TemplateWord tw = new TemplateWord();
                tw.setFileUuid(uuid);
                tw.setId(Integer.parseInt(uploadingId));
                List<TemplateWord> a = technicalAdviceService.consultList(map);
                messageBean.setType(MessageConstant.SUCCESS_TYPE);
                messageBean.setCode(MessageConstant.SUCCESS);
                messageBean.setMessage("查询征询类列表成功");
                messageBean.setData(a);
            }
        } catch (Exception e) {
            e.printStackTrace();
            messageBean.setType(MessageConstant.FAIL_TYPE);
            messageBean.setCode(MessageConstant.FAIL);
            messageBean.setMessage("查询列表失败");
        }
        return messageBean;
    }

    /**
     * 技审草稿上传————————上传技审意见word文档
     */
    @RequestMapping("technicalfile")
    public MessageBean technicalfile(@RequestHeader(value = TOKEN_HEADER, defaultValue = "") String token, String uuid, String reviewType, String no, @RequestParam("file") MultipartFile file) throws Exception {
        MessageBean messageBean = new MessageBean();
        Date date = new Date();
        //token为空的情况下 vv
        if (StringUtils.isNotEmpty(token) == false) {
            messageBean.setType(MessageConstant.FAIL_TYPE);
            messageBean.setCode(MessageConstant.FAIL);
            messageBean.setMessage("token为空生成失败");
            return messageBean;
        }
        String userNumber = JwtTokenUtils.getUserNumber(token.substring(JwtTokenUtils.TOKEN_PREFIX.length()));
        try {
        //获取文件后缀名
        int begin = file.getOriginalFilename().indexOf(".");
        int last = file.getOriginalFilename().length();
        String a = file.getOriginalFilename().substring(begin, last);
        if (file.isEmpty()) {
            messageBean.setType(MessageConstant.FAIL_TYPE);
            messageBean.setCode(MessageConstant.FAIL);
            messageBean.setMessage("上传文件为空");
            return messageBean;
        }
        String contentType1 = file.getContentType();
        if (!contentType1.contains("application/vnd.openxmlformats-officedocument.wordprocessingml.document")) {
            messageBean.setType(MessageConstant.FAIL_TYPE);
            messageBean.setCode(MessageConstant.FAIL);
            messageBean.setMessage("上传文件不为docx，请重新上传");
            return messageBean;
        }

        //1、上传非docx文件格式
        Map<String, String> type = technicalAdviceService.uploadstatus(file, uuid, reviewType);
        String code = type.get("code");
        String message = type.get("message");
        messageBean.setCode(code);
        messageBean.setMessage(message);
        } catch (Exception e) {
            e.printStackTrace();
            messageBean.setType(MessageConstant.FAIL_TYPE);
            messageBean.setCode(MessageConstant.FAIL);
            messageBean.setMessage("上传技审文件失败");
        }
        return messageBean;
    }

    /**
     * 附件上传————上传补充的相关的资料
     */
    @RequestMapping("accessoryfile")
    public MessageBean accessoryupload(String uuid, HttpServletRequest request) {
        MessageBean messageBean = new MessageBean();
        List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
        TechnicalAccessory ta = new TechnicalAccessory();
        try {
            List<String> list = new ArrayList<>();
            for (int i = 0; i < files.size(); i++) {
                MultipartFile file = files.get(i);
                InputStream is = file.getInputStream(); //得到文件流
                String fileName = file.getOriginalFilename(); //文件名
                list.add(fileName);
                String contentType = file.getContentType();  //类型
                if (!file.isEmpty()) {
                    //把文件放置Minio桶(文件夹)
                    String fileI = UUIDUtils.getUUID();
                    Date date = new Date();
                    ta.setAessId(fileI);
                    ta.setAessName(fileName);
                    ta.setAessUuid(uuid);
                    ta.setAessTime(date);
                    technicalAdviceService.accessoryupload(ta);
                    minIoUtil.putObject("projectotherattach", fileName, is, contentType);
                }
            }
            messageBean.setData(list);
            messageBean.setType(MessageConstant.SUCCESS_TYPE);
            messageBean.setCode(MessageConstant.SUCCESS);
            messageBean.setMessage("上传文件成功");
        } catch (Exception e) {
            e.printStackTrace();
            messageBean.setType(MessageConstant.FAIL_TYPE);
            messageBean.setCode(MessageConstant.FAIL);
            messageBean.setMessage("上传文件失败");
        }
        return messageBean;
    }


    /**
     * 删除数据库某条数据
     * 同时多线程后台进程删除数据
     */
    @RequestMapping(value = "/technicaldelect")
    public MessageBean technicaldelect(@RequestHeader(value = TOKEN_HEADER, defaultValue = "") String token, @RequestBody Map<String, String> map) {
        MessageBean messageBean = new MessageBean();

        String fileid = map.get("fileid");
        TemplateWord tw = new TemplateWord();
        tw.setFileId(fileid);
        List<TemplateWord> templateWords = technicalAdviceService.templatId(tw);
        //根据获取技审表的uuid
        String uuid = templateWords.get(0).getFileUuid();
        TechnicalHistory technicalHistory = new TechnicalHistory();
        Date date = new Date();
        //token为空的情况下
        if (StringUtils.isNotEmpty(token) == false) {
            messageBean.setType(MessageConstant.FAIL_TYPE);
            messageBean.setCode(MessageConstant.FAIL);
            messageBean.setMessage("token为空生成失败");
            return messageBean;
        }
        String userNumber = JwtTokenUtils.getUserNumber(token.substring(JwtTokenUtils.TOKEN_PREFIX.length()));
        map.put("userNumber", userNumber);
        map.put("fileUuid", uuid);
        try {
            //项目编号
            technicalAdviceService.templatdelect(map);
            messageBean.setType(MessageConstant.SUCCESS_TYPE);
            messageBean.setCode(MessageConstant.SUCCESS);
            messageBean.setMessage("技审意见删除成功");
            messageBean.setData(1);
        } catch (Exception e) {
            //历史记录变更
            technicalHistory.setCreateTime(date);
            technicalHistory.setOperationalType("3");
            technicalHistory.setOperationalContext("技审意见删除");
            technicalHistory.setOperationalOperator(userNumber);
            technicalHistory.setOperationalState("0");
            technicalHistory.setOperationalProject(uuid);
            technicalAdviceService.serverTechnicalHistory(technicalHistory);
            messageBean.setType(MessageConstant.FAIL_TYPE);
            messageBean.setCode(MessageConstant.FAIL);
            messageBean.setMessage("技审意见删除失败");
        }
        return messageBean;
    }

    /**
     * 技审草稿列表接口--技审列表
     */
    @RequestMapping(value = "/jslist")
    public MessageBean technicalzxfalist(@RequestBody Map<String, String> map) {
        MessageBean messageBean = new MessageBean();
        try {
            String fileUuid = map.get("fileUuid");
            //项目编号
            List<TechnicalList> technicalzxfalist = technicalAdviceService.technicalzxfalist(fileUuid);
            messageBean.setType(MessageConstant.SUCCESS_TYPE);
            messageBean.setCode(MessageConstant.SUCCESS);
            messageBean.setMessage("列表查询成功");
            messageBean.setData(technicalzxfalist);
        } catch (Exception e) {
            e.printStackTrace();
            messageBean.setType(MessageConstant.FAIL_TYPE);
            messageBean.setCode(MessageConstant.FAIL);
            messageBean.setMessage("列表查询失败");
        }
        return messageBean;
    }

    /**
     * 技审草稿列表接口--专项保护方案列表
     */
    @RequestMapping(value = "/zxlist")
    public MessageBean technicalzxfazxfalist(@RequestBody Map<String, String> map) {
        MessageBean messageBean = new MessageBean();
        try {
            String no = map.get("no");
            //项目编号
            List<Technicalzxlist> technicalzxfalist = technicalAdviceService.technicalzxfazxfalist(no);
            messageBean.setType(MessageConstant.SUCCESS_TYPE);
            messageBean.setCode(MessageConstant.SUCCESS);
            messageBean.setMessage("列表查询成功");
            messageBean.setData(technicalzxfalist);
        } catch (Exception e) {
            e.printStackTrace();
            messageBean.setType(MessageConstant.FAIL_TYPE);
            messageBean.setCode(MessageConstant.FAIL);
            messageBean.setMessage("列表查询失败");
        }
        return messageBean;
    }


    /**
     * 判断word文档中是否有图片
     * 传参为 文件ID
     */
    @RequestMapping(value = "/pictureSeal")
    public MessageBean TechnicalAuditPictureSeal(@RequestParam("file") MultipartFile file, String fileType) {
        MessageBean messageBean = new MessageBean();
        try {
            Integer a = technicalAdviceService.wordIncludePic(file, fileType);
            if (a == 1) {
                messageBean.setType(MessageConstant.FAIL_TYPE);
                messageBean.setCode(MessageConstant.FAIL);
                messageBean.setMessage("Word文档无图片");
            } else if (a == 2) {
                messageBean.setType(MessageConstant.SUCCESS_TYPE);
                messageBean.setCode(MessageConstant.SUCCESS);
                messageBean.setMessage("Word文档有图片");
            } else if (a == 3) {
                messageBean.setType(MessageConstant.FAIL_TYPE);
                messageBean.setCode(MessageConstant.FAIL);
                messageBean.setMessage("Word信息异常");
            } else if (a == 4) {
                messageBean.setType(MessageConstant.SUCCESS_TYPE);
                messageBean.setCode(MessageConstant.SUCCESS);
                messageBean.setMessage("PDF有盖章信息");
            } else if (a == 5) {
                messageBean.setType(MessageConstant.FAIL_TYPE);
                messageBean.setCode(MessageConstant.FAIL);
                messageBean.setMessage("PDF无盖章信息");
            } else if (a == 6) {
                messageBean.setType(MessageConstant.FAIL_TYPE);
                messageBean.setCode(MessageConstant.FAIL);
                messageBean.setMessage("PDF信息异常");
            } else if (a == 0) {
                messageBean.setType(MessageConstant.FAIL_TYPE);
                messageBean.setCode(MessageConstant.FAIL);
                messageBean.setMessage("文件格式错误");
            } else {
                messageBean.setType(MessageConstant.FAIL_TYPE);
                messageBean.setCode(MessageConstant.FAIL);
                messageBean.setMessage("文档信息异常");
            }
        } catch (Exception e) {
            e.printStackTrace();
            messageBean.setType(MessageConstant.FAIL_TYPE);
            messageBean.setCode(MessageConstant.FAIL);
            messageBean.setMessage("文档信息异常");
        }
        return messageBean;
    }


    /**
     * 工程信息概括退回
     */
    @RequestMapping(value = "/processFlow")
    public MessageBean processFlow(@RequestHeader(value = TOKEN_HEADER, defaultValue = "") String token, @RequestBody Map<String, String> map) {
        MessageBean messageBean = new MessageBean();
        //token为空的情况下
        if (StringUtils.isNotEmpty(token) == false) {
            messageBean.setType(MessageConstant.FAIL_TYPE);
            messageBean.setCode(MessageConstant.FAIL);
            messageBean.setMessage("token为空生成失败");
            return messageBean;
        }
        try {
            Map<String, String> a = technicalAdviceService.updateFeedback(map);
            String code = a.get("code");
            String message = a.get("message");
            messageBean.setCode(code);
            messageBean.setMessage(message);
        } catch (Exception e) {
            messageBean.setType(MessageConstant.FAIL_TYPE);
            messageBean.setCode(MessageConstant.FAIL);
            messageBean.setMessage("技审状态变更失败");
        }
        return messageBean;
    }


    /**
     * 征询类的技审列表
     */
    @RequestMapping("/consultList")
    public MessageBean consultList(@RequestBody Map<String, String> map) {
        MessageBean messageBean = new MessageBean();
        try {
            String uuid = map.get("uuid");
            String uploadingId = map.get("type");
            TemplateWord tw = new TemplateWord();
            tw.setFileUuid(uuid);
            tw.setId(Integer.parseInt(uploadingId));
            List<TemplateWord> a = technicalAdviceService.consultList(map);
            messageBean.setType(MessageConstant.SUCCESS_TYPE);
            messageBean.setCode(MessageConstant.SUCCESS);
            messageBean.setMessage("查询征询类列表成功");
            messageBean.setData(a);
        } catch (Exception e) {
            e.printStackTrace();
            messageBean.setType(MessageConstant.FAIL_TYPE);
            messageBean.setCode(MessageConstant.FAIL);
            messageBean.setMessage("查询征询类列表失败");
        }
        return messageBean;
    }


    /**
     * 更新技审生成记录接口
     */
    @RequestMapping(value = "/updateQueryLine")
    public MessageBean updateQueryLine(@RequestBody Map<String, String> map) {
        MessageBean messageBean = new MessageBean();
        try {
            Integer a = technicalAdviceService.updateflow(map);
            messageBean.setType(MessageConstant.SUCCESS_TYPE);
            messageBean.setCode(MessageConstant.SUCCESS);
            messageBean.setMessage("修改技审记录成功");
            messageBean.setData(a);
        } catch (Exception e) {
            e.printStackTrace();
            messageBean.setType(MessageConstant.FAIL_TYPE);
            messageBean.setCode(MessageConstant.FAIL);
            messageBean.setMessage("修改技审记录失败");
        }
        return messageBean;
    }


    /**
     * 技审生成记录接口
     */
    @RequestMapping(value = "/serverQueryLine")
    public MessageBean serverQueryLine(@RequestBody Map<String, String> map) {
        MessageBean messageBean = new MessageBean();
        try {
            Map<String, String> a = technicalAdviceService.serverQueryLine(map);
            messageBean.setType(MessageConstant.SUCCESS_TYPE);
            messageBean.setCode(MessageConstant.SUCCESS);
            messageBean.setMessage("查询技审记录成功");
            messageBean.setData(a);
        } catch (Exception e) {
            e.printStackTrace();
            messageBean.setType(MessageConstant.FAIL_TYPE);
            messageBean.setCode(MessageConstant.FAIL);
            messageBean.setMessage("查询技审记录失败");
        }
        return messageBean;
    }






}
